package com.ywz.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywz.common.ResultResp;
import com.ywz.common.StringUtils;
import com.ywz.project.base.system.entity.TSysMenu;
import com.ywz.project.base.system.entity.TSysPermission;
import com.ywz.project.base.system.service.TSysMenuService;
import com.ywz.project.base.system.service.TSysPermissionService;
import com.ywz.project.system.dto.req.PermissionPageReq;
import com.ywz.project.system.dto.resp.PermissionPageResp;
import com.ywz.project.system.service.SysPermissionApiService;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysPermissionApiServiceImpl implements SysPermissionApiService {
    @Resource
    private TSysPermissionService tSysPermissionService;
    @Resource
    private TSysMenuService tSysMenuService;

    @Override
    public ResultResp getPermissionPage(PermissionPageReq req) {
        LambdaQueryWrapper<TSysPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TSysPermission::getIsDeleted, 0);
        if (!StringUtils.isEmpty(req.getPermissionName())) {
            wrapper.like(TSysPermission::getPermissionName, req.getPermissionName());
        }
        Page<TSysPermission> page = new Page<>(req.getPageNum(), req.getPageSize());
        Page<PermissionPageResp> respPage = new Page<>();
        // 获取权限列表
        Page<TSysPermission> permissionPage = tSysPermissionService.page(page, wrapper);
        // 获取菜单列表
        List<TSysMenu> menuList = tSysMenuService.list();
        // 转换返回对象
        respPage.setTotal(permissionPage.getTotal());
        respPage.setCurrent(permissionPage.getCurrent());
        respPage.setSize(permissionPage.getSize());
        respPage.setRecords(permissionPage.getRecords().stream().map(permission -> {
            PermissionPageResp resp = new PermissionPageResp();
            BeanUtils.copyProperties(permission, resp);
            // 获取菜单名称
            if (!StringUtils.isEmpty(permission.getMenuId())) {
                String[] split = permission.getMenuId().split(StringUtils.SEPARATOR);
                // 获取菜单名称
                String menuName = menuList.stream().sorted(Comparator.comparing(TSysMenu::getId)).filter(menu -> {
                    boolean flag = false;
                    for (String s : split)
                        if (s.equals(menu.getId().toString())) {
                            flag = true;
                            break;
                        }
                    return flag;
                }).map(TSysMenu::getMenuName).collect(Collectors.joining("-"));
                resp.setMenuName(menuName);
            }
            return resp;
        }).collect((Collectors.toList())));
        return ResultResp.success(respPage);
    }

    @Override
    public ResultResp getPermissionListByRoleId(String roleIds) {
        if (!StringUtils.isNumberAndSeparator(roleIds))
            return ResultResp.error("角色ids拼接格式错误");
        String[] split = roleIds.split(StringUtils.SEPARATOR);
        List<Integer> roleIdsList = Arrays.stream(split).map(Integer::parseInt).collect(Collectors.toList());
        List<TSysPermission> permissionListByRoleIds = tSysPermissionService.getPermissionListByRoleIds(roleIdsList);
        return ResultResp.success(permissionListByRoleIds);
    }

    @Override
    public ResultResp editPermission(TSysPermission req) {
        req.setUpdateTime(LocalDateTime.now());
        boolean saved = tSysPermissionService.saveOrUpdate(req);
        if (saved)
            return ResultResp.success();
        return ResultResp.error();
    }

    @Override
    public ResultResp removePermission(String ids) {
        if(StringUtils.isNumberAndSeparator(ids)){
            String[] split = ids.split(StringUtils.SEPARATOR);
            List<Integer> idsList = Arrays.stream(split).map(Integer::parseInt).collect(Collectors.toList());
            LambdaUpdateWrapper<TSysPermission> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.in(TSysPermission::getId, idsList).set(TSysPermission::getIsDeleted, 1);
            updateWrapper.set(TSysPermission::getUpdateTime, LocalDateTime.now());
            boolean saved = tSysPermissionService.update(updateWrapper);
            if (saved)
                return ResultResp.success();
            return ResultResp.error("删除失败");
        }else{
            return ResultResp.error("ids拼接格式错误");
        }
    }

    @Override
    public ResultResp getPermissionList() {
        LambdaQueryWrapper<TSysPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TSysPermission::getIsDeleted, 0);
        return ResultResp.success(tSysPermissionService.list(queryWrapper));
    }
}
